package com.zscat.mallplus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zscat.mallplus.annotation.IgnoreAuth;
import com.zscat.mallplus.annotation.SysLog;
import com.zscat.mallplus.dms.entity.DmsOrder;
import com.zscat.mallplus.dms.service.*;
import com.zscat.mallplus.enums.OrderStatus;
import com.zscat.mallplus.qsg.*;
import com.zscat.mallplus.ums.entity.UmsMember;
import com.zscat.mallplus.ums.service.IUmsMemberService;
import com.zscat.mallplus.utils.CommonResult;
import com.zscat.mallplus.utils.DateUtil;
import com.zscat.mallplus.utils.UserUtils;
import com.zscat.mallplus.vo.MemberDetailVo;
import com.zscat.mallplus.vo.OrderVo;
import com.zscat.mallplus.vo.ReportVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.awt.*;
import java.lang.reflect.Member;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 门店会员管理Controller
 */
@RestController
@Api(tags = "QsgMemberStoreController", description = "门店会员管理")
@RequestMapping("/api/qsg/memberstore")
public class QsgMemberStoreController {

    @Autowired
    private IQsgStoreService storeService;
    @Autowired
    private IQsgMemberStoreService memberStoreService;
    @Autowired
    private IUmsMemberService memberService;
    @Autowired
    private IntegralService integralService;
    @Autowired
    private IQsgRechargeOrderService rechargeOrderService;
    @Autowired
    private IDmsOrderService orderService;
    @Autowired
    private IDeviceService deviceService;

    @IgnoreAuth
    @PostMapping(value = "/list")
    @ApiOperation(value = "门店会员列表")
    public Object list(@RequestBody UmsMember entity) {
        try {
            List<QsgMemberStore> memberStores = memberStoreService.getPage(entity.getPhone(),entity.getPageIndex(),entity.getPageSize(),entity.getStoreId());
            return new CommonResult().success(memberStores);
        }catch (Exception e){
            e.printStackTrace();
            return new CommonResult().failed();
        }
    }

    /**
     *
     * @param status 0:充值订单/1:现金订单/2:余额订单
     * @param memberId
     * @param pageSize
     * @param pageNum
     * @return
     */
    @IgnoreAuth
    @SysLog(MODULE = "dms", REMARK = "查询教练订单列表")
    @ApiOperation(value = "查询教练订单列表")
    @GetMapping(value = "/memberdetail")
    public Object coachOrderList(@RequestParam String status,@RequestParam Long memberId,@RequestParam Integer storeId,
                                 @RequestParam(value = "pageSize", required = false, defaultValue = "30") Integer pageSize,
                                 @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum) {
        List<MemberDetailVo> vos = new ArrayList<>();
        if (status.equalsIgnoreCase("0")){
            QueryWrapper qw = new QueryWrapper();
            qw.eq("order_status",2);
            qw.eq("member_id",memberId);
            qw.eq("store_id",storeId);
            qw.orderByDesc("create_datetime");
            IPage<QsgRechargeOrder> rechargeOrderIPage = rechargeOrderService.page(new Page<>(pageNum, pageSize), qw);
            vos = toMemberDetail(rechargeOrderIPage.getRecords(),null);
        }
        if (status.equalsIgnoreCase("1")){
            QueryWrapper qw = new QueryWrapper();
            List<Integer> statues = new ArrayList<>();
            statues.add(2);
            statues.add(7);
            qw.eq("integral",0);
            qw.in("order_status",statues);
            qw.eq("member_id",memberId);
            qw.eq("store_id",storeId);
            qw.orderByDesc("create_datetime");
            IPage<DmsOrder> orderIPage = orderService.page(new Page<>(pageNum,pageSize),qw);
            vos = toMemberDetail(null,orderIPage.getRecords());
        }
        if (status.equalsIgnoreCase("2")){
            QueryWrapper qw = new QueryWrapper();
            List<Integer> statues = new ArrayList<>();
            statues.add(2);
            statues.add(7);
            qw.ne("integral",0);
            qw.in("order_status",statues);
            qw.eq("member_id",memberId);
            qw.eq("store_id",storeId);
            qw.orderByDesc("create_datetime");
            IPage<DmsOrder> orderIPage = orderService.page(new Page<>(pageNum,pageSize),qw);
            vos = toMemberDetail(null,orderIPage.getRecords());
        }
        return new CommonResult().success(vos);
    }

    private List<MemberDetailVo> toMemberDetail(List<QsgRechargeOrder> rechargeOrders,List<DmsOrder> orders){
        List<MemberDetailVo> vos = new ArrayList<>();
        if (null!=rechargeOrders&&rechargeOrders.size()>0){
            for (QsgRechargeOrder rechargeOrder : rechargeOrders){
                MemberDetailVo vo = new MemberDetailVo();
                QsgStore store = storeService.getById(rechargeOrder.getStoreId());
                vo.setCreateDatetime(rechargeOrder.getCreateDatetime());
                if (null!=store){
                    vo.setName(store.getName());
                }else {
                    vo.setName("门店不存在");
                }
                vo.setOrderSn(rechargeOrder.getOrderSn());
                vo.setPrice(rechargeOrder.getPrice());
                vos.add(vo);
            }
        }
        if (null!=orders&&orders.size()>0){
            for (DmsOrder order : orders){
                MemberDetailVo vo = new MemberDetailVo();
                QsgDevice device = deviceService.getById(order.getDeviceId());
                vo.setCreateDatetime(order.getCreateDatetime());
                if (null!=device){
                    vo.setName(device.getMachineName());
                }else {
                    vo.setName("房间不存在");
                }
                vo.setOrderSn(order.getOrderSn());
                vo.setPrice(order.getPrice());
                vos.add(vo);
            }
        }
        return vos;
    }
}
